<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>let var const关键字的区别</title>
</head>

<body>
  <script>
    /*     var a = 100;
    
        if (true) {
          var b = 1000;
          let c = 10
          // let c = 'x' //Identifier 'c' has already been declared
          var b = 'b'
        }
    
        console.log(b); // 1000
        // console.log(c); //c is not defined
    
        let x = "x"
        console.log(window.x); //undefined
     */

    // console.log(a); //Cannot access 'a' before initialization
    // let a = 100;

    /* 
        for (var i = 1; i <= 5; i++) {
          setTimeout(function () {
            console.log(i); //6 6 6 6 6
          }, 0)
        }
    
        // 循环变量为6时，退出循环。
        // 定时器执行时，读取I变量的值，值为6.
        console.log(i); // 6
        setTimeout(function () {
          console.log(i); //6
        }, 0
        )
     */

    // for (let i = 1; i <= 5; i++) {
    //   setTimeout(function () {
    //     console.log(i); // 1,2,3,4,5
    //   }, 0)
    // }
    // console.log(i); // ReferenceError: i is not defined
    /* 
        {
          let i = 1; // let 声明的变量，有严格的作用域
          var a = 2;
        }
    
        console.log(i); //ReferenceError: i is not defined
        console.log(a);
     */

    // const x; //Missing initializer in const declaratio
    // const x = "asdfa0";
    // x = 123; //TypeError: Assignment to constant variable.

    const obj = { a: 1, b: 2 };
    obj.a = 111;
    obj.b = 111;

    console.log(obj); //{a: 111, b: 111}

    // obj = {} //TypeError: Assignment to constant variable.

    const arr1 = [];
    arr1.push(1)
    console.log(arr1); //[1]
    // arr1 = ["12123"]  //Assignment to constant variable.

    // 对象，数组，不可以重修赋值新的对象和数组

    const fn1 = function () {

    }
// TypeError: Assignment to constant variable.
    // fn1 = 1;
    // fn1 = function () { }
  </script>
</body>

</html>